﻿//Remove it before deployment
#define On_Win_Emulator

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;


namespace Controller.Common
{
    /// <summary>
    /// Retrieve and store co-ordinates.
    /// </summary>
    public class GPSController
    {
        #region Variables

        /// <summary>
        /// Current State of the Location Service 
        /// </summary>
        public string GPS_Status_Text
        { get; private set; }
        /// <summary>
        /// Current State of the Location Service. Location Service will be only available if it is true.
        /// </summary>
        public bool GPS_Status
        { get; private set; }
        /// <summary>
        /// Altitude with reference to sea-level at current position
        /// </summary>
        public double Altitude
        { get; private set; }
        /// <summary>
        /// Heading Speed
        /// </summary>
        public double Speed
        { get; private set; }
        /// <summary>
        /// Latitude value of current position
        /// </summary>
        public double Latitude
        { get; private set; }
        /// <summary>
        /// Longitude value of current position
        /// </summary>
        public double Lontitude
        { get; private set; }
        /// <summary>
        /// TimeStamp
        /// </summary>
        public DateTimeOffset TimeStamp
        { get; private set; }

        #endregion

        #region Event Handlers 

        public event EventHandler Position_Changed;
        public event EventHandler GPS_StatusChanged;

        #endregion

#if On_Win_Emulator
        GpsEmulatorClient.GeoCoordinateWatcher _watcher;
#else
        GeoCoordinateWatcher _watcher;
#endif

        /// <summary>
        /// Initializes the GPS and gets position data
        /// </summary>
        public void start_GeoWatcher()
        {
            if (_watcher == null)
            {
                #if On_Win_Emulator
                _watcher = new GpsEmulatorClient.GeoCoordinateWatcher(GeoPositionAccuracy.High);
                #else 
                _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
                #endif
                _watcher.MovementThreshold = 20;

                _watcher.PositionChanged += _watcher_PositionChanged;
                _watcher.StatusChanged += _watcher_StatusChanged;
            }
            if (!GPS_Status)
            {
               _watcher.Start();
            }
        }

        /// <summary>
        /// Stop location services
        /// </summary>
        public void stop_GeoWatcher()
        {
            if (_watcher != null)
            {
                _watcher.Stop();
            }
        }

        /// <summary>
        /// Gets the Co-Ordinates for current location. Caller should register for the status change event to 
        /// get notification
        /// </summary>
        public void Get_Current_Position_Coordinates()
        {
            start_GeoWatcher();
            _watcher.StatusChanged += OneTimeQuery_GPS_StatusChanged;
        }

       

        /* Event Handlers*/

        void OneTimeQuery_GPS_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                _watcher.StatusChanged -= OneTimeQuery_GPS_StatusChanged;
                _watcher.Stop();
            }
        }
        
        void _watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Latitude = e.Position.Location.Latitude;
            Lontitude = e.Position.Location.Longitude;
            Speed = e.Position.Location.Speed;
            Altitude = e.Position.Location.Altitude;
            TimeStamp = e.Position.Timestamp;

            if (Position_Changed != null)
            {
                Position_Changed(this, new EventArgs());
            }
         }

        void _watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Ready:
                    // The Location Service is working and is receiving location data.
                    GPS_Status_Text = "Location Service is Up and running.";
                    GPS_Status = true;
                    if (GPS_StatusChanged != null)   //GPS data is only available when status == ready
                    {
                        GPS_StatusChanged(this, new EventArgs());
                    }
                    break;
                                   
                case GeoPositionStatus.Initializing:
                    // The Location Service is initializing.
                    GPS_Status_Text = "Location Service is currently initializing";
                    GPS_Status = false;
                    break;

                case GeoPositionStatus.NoData:
                    // The Location Service is working, but it cannot get location data.
                    GPS_Status_Text = "Location Data not available.";
                    GPS_Status = false;
                    break;

                case GeoPositionStatus.Disabled:
                    // The Location Service is disabled or unsupported.
                    // Check to see if the user has disabled the Location Service.
                    if (_watcher.Permission == GeoPositionPermission.Denied)
                    {
                        // The user has disabled the Location Service on their device.
                        GPS_Status_Text = "Application does not have access to Location Services";
                    }
                    else
                    {
                        GPS_Status_Text = "Location Service is not functioning on this device";
                    }
                    GPS_Status = false;
                    break;
            }
            
        }
    }
}
